<?php
/**
 * utility class for date time processing
 * as a utility, it has not inheritance whatsoever,
 * so it stays static for the best performance
 *
 * must be manually included.
 *
 * @static
 *
 * @package com.commentor
 * @author  Bruce Li <toughgamer>
 *
 * @copyright 2008 commentor.com.au
 */
class Util_Date
{
    /**
     * get timestamp, default as current
     *
     * @param int $timestamp the date format
     *
     * @access public
     * @return str date
     */
    static public function getTimeStamp($timestamp = 0)
    {
        if ($timestamp > 0) {
            return date('Y-m-d H:i:s', (int) $timestamp);
        } else {
            return date('Y-m-d H:i:s');
        }

    }// end getTimeStamp


    /**
     * format timestamp by given format
     *
     * @param str $timestamp mysql timestamp
     * @param str $format	 format string
     *
     * @return formatted string
     */
    static public function formatTimeStamp($timestamp, $format = 'm D')
    {
        // create object
        $datetime = strtotime($timestamp);
        return date($format, $datetime);

    }// end formatTimeStamp


    /**
     * calculate the difference between 2 timestamps
     *
     * @param timestamp $date1  first date
     * @param timestamp $date2  second date
     * @param str	    $format the actual format
     *
     * @return str the time with format
     */
    static public function dateDiff($date1, $date2, $format = null)
    {
        $dateStart = strtotime($date1);
        $dateEnd   = strtotime($date2);

        $diff = ($dateStart - $dateEnd);

        if (!$format) {
            return $diff;
        }

        // otherwise, format it and return
        return strftime($format, $diff);

    }// end dateDiff


    /**
     * get date by given timezone
     * @param str $date date string
     * @param int $difference difference please use hours... +1100 or -500
     * @param str $format the actual format, default to 'Y-m-d H:i:s'
     * @return str formatted date..
     */
    static public function getDateByTimezone($date, $difference = 0, $format = 'Y-m-d H:i:s')
    {
        // date must be not null
        if (empty($date)) {
            return 'ERROR, Invalid Date';
        }

        // would be nice if date_create is supported. but alas, we are using 5.1...
        if ($difference != 0 && abs($difference) < 100) {
            // should be exception but let's be gentle...
            return 'ERROR, Invalid Difference';
        }
        // set string to time
        $time = strtotime($date . ' GMT');
        // redo by using difference
        $time += 36 * $difference;
        // go back to spit it out!
        return gmdate($format, $time);  // why gm? cos it's already changed dude!

    }// end getDateByTimezone


    // simply get the gmdate for storage
    static public function getGmdate()
    {
        return gmdate('Y-m-d H:i:s');

    }// end getGmdate


    /**
     * get date with given template
     *
     * @param timestamp $date1    first date
     * @param timestamp $date2    second date
     * @param str 		$format	  format string
     * @param str 	    $template the template to display
     *
     * @return str the formatted date string
     */
    static function getDates($date1, $date2, $format = 'm D', $template = ':start to :end')
    {
        // compare dates and then do things according ly...
        if (empty($date1) && empty($date2)) {
            return null;
        }

        if (empty($date1)) {
            return self::formatTimeStamp($date2, $format);
        }

        if (empty($date2)) {
            return self::formatTimeStamp($date1, $format);
        }

        if ($date1 == $date2) {
            // then just one
            return self::formatTimeStamp($date1, $format);
        }

        // else, if same month
        if (self::formatTimeStamp($date1, 'm') ==
            self::formatTimeStamp($date2, 'm')) {
            // then match two together...
            return str_replace(array(':start', ':end'),
                               array(self::formatTimeStamp($date1, 'j'),
                                     self::formatTimeStamp($date2, 'j')),
                               $template
                               ) . self::formatTimeStamp($date1, ' M y');
        }

        // build format 1/2 based on yr...
        $format1 = $format2 = $format;

        if (strpos($format, 'y') !== false) {
            $format1 = str_replace('y', '', $format);
        }

        // else, just as normal:
        return str_replace(array(':start', ':end'),
                           array(self::formatTimeStamp($date1, $format1),
                                 self::formatTimeStamp($date2, $format2)),
                           $template
                           );

    }// end getDates

}// end commentor_DateProcessor
?>